{{#contentFor "Scripts"}}
<script type="text/javascript" src="/scripts/common.js"></script>
<script type="text/javascript">
    $(function () {
        CheckECSStatus();

        $('[db-index]').each(function () {
            var self = $(this);
            var dbIndex = self.attr('db-index');
            var dbType = self.attr('db-type');

            var panelBody = self.find('.card-body');

            $.ajax({
                url: `/api/${dbType}/${dbIndex}/dbs`,
                type: 'GET',
                cache: false,
                dataType: 'json',
                success: function (rs) {
                    if (!rs.success) {
                        panelBody.html(rs.message);
                    } else {
                        panelBody.html('<p class="text-success">数据库连接成功（数据库状态信息如下所示）！</p>');

                        rs.datas.forEach(function(item){
                            var text = item.SCHEMA_NAME;
                            panelBody.append(`<p><a href="/db/tables?dbIndex=${dbIndex}&dbType=${dbType}&schema=${text}">${text}</a></p>`);
                        });
                    }
                }
            });
        });

        function CheckECSStatus() {
            $('[ecs-id]').each(function () {
                var self = $(this);
                var b = self.find('.server-status > b').eq(0);
                var cesId = self.attr('ecs-id');

                b.text('查询中...');

                $.ajax({
                    url: '/api/hwcloud/ecs/status/'+cesId,
                    type: 'GET',
                    dataType: 'json',
                    success: function (rs) {
                        b.text(rs.server.status == 'ACTIVE' ? '正常' : '异常')
                            .addClass(`text-${(rs.server.status == 'ACTIVE' ? 'success' : 'danger')}`);

                        b.parent().after('<p>服务器信息查询中...</p>');

                        $.ajax({
                            url: '/api/hwcloud/ecs/'+cesId+'/detail/' + rs.server.flavor.id,
                            type: 'GET',
                            dataType: 'json',
                            success: function (rs2) {
                                b.parent().next().remove();

                                b.parent()
                                 .after('<p>逻辑处理器：<b>' + rs2.flavor.vcpus + ' 核</b></p>')
                                 .after('<p>内存容量：<b>' + (rs2.flavor.ram / 1024) + ' GB</b></p>')
                                 .after('<p>硬盘容量：<b>' + (rs2.flavor.disk || '不限制') + '</b></p>');
                            }
                        });
                    },
                    error: function (err) {
                        b.text('出现错误');
                    }
                });
            });
        }
    });

</script>
{{/contentFor}}

<p class="alert alert-primary">数据库服务器</p>
<div class="row">
    {{#each dbs}}
    <div class="col-sm-4">
        <div class="card card-primary" db-index="{{@index}}" db-type="{{type}}">
            <div class="card-header has-button">
                <span class="pull-left">{{name}}</span>
                <span class="pull-right">
                    <a class="btn btn-primary" href="/db/status/{{@index}}">
                        <i class="bi bi-info-circle-fill"></i> 服务器状态
                    </a>
                </span>
            </div>
            <div class="card-body" style="overflow: auto;height:200px;">
                --
            </div>
        </div>
    </div>
    {{/each}}
</div>
<p class="alert alert-danger">弹性云服务器状态</p>
<div class="row">
    {{#each servers }}
    <div class="col-sm-3">
        <div class="card card-primary" ecs-id="{{id}}">
            <div class="card-header">【{{describle}}】{{name}}</div>
            <div class="card-body">
                <p><b>服务器所在区域：{{regionName}}</b></p>
                <p>内网IP地址：{{innerIP}}</p>
                <p>外网IP地址：{{outerIP}}</p>
                <p class="server-status">服务器状态：<b>--</b></p>
            </div>
        </div>
    </div>
    {{/each}}
</div>
